local erfeng = fk.CreateSkill{
    name = "ym1__erfeng",
}
local erfeng_ban = {"steam__qisu","rfenghou__jianwang","rfenghou__jianwangg","rmt__jianwang","rmt__jianwangg","rfenghou__bidao"}
erfeng:addEffect("active",{
    mute = true,
    target_num = 1,
    card_num = 0,
    target_filter = function (self, player, to_select, selected, selected_cards)
        return to_select ~= player
    end,
    card_filter = Util.FalseFunc,
    can_use = function (self, player)
        return player:hasSkill(erfeng.name) and player:usedSkillTimes("ym1__erfeng",Player.HistoryRound) < 2 and player:usedSkillTimes("erfeng_active",Player.HistoryPhase) < 1
    end,
    times = function(self,player)
        return 2 - player:usedSkillTimes("ym1__erfeng",Player.HistoryRound)
    end,
    on_use = function (self, room, effect)
        local player = room:getPlayerById(effect.from.id)
        local to = room:getPlayerById(effect.tos[1].id)
        player:addSkillUseHistory("erfeng_active",1)
        local player_skills = {}
        for _, skill in ipairs(player.player_skills) do
            if skill:isPlayerSkill(player) and not skill.name:startsWith("#") and not skill.attached_equip then
                table.insert(player_skills, skill.name)
            end
        end
        room:notifySkillInvoked(player,erfeng.name)
        player:broadcastSkillInvoke(erfeng.name,math.random(1,2))
        room:handleAddLoseSkills(player,"-"..player_skills[#player_skills],nil,true,false)
        --[[local mark_skills = type(player:getMark("ym1__erfeng")) == "table" and player:getMark("ym1__erfeng") or {}
        for i, v in ipairs(mark_skills) do
            if table.contains(v[2],player_skills[#player_skills]) then
                table.removeOne(v[2],player_skills[#player_skills])
                if #v[2] > 0 then
                    local realNames = table.map(v[2], Util.TranslateMapper)
                    local target = room:getPlayerById(v[1])
                    room:setPlayerMark(player,"@ym1__erfeng_"..tostring(i),Fk:translate(target.general).." <font color='burlywood'>" .. table.concat(realNames, " ") .. "</font>")
                else
                    room:setPlayerMark(player,"@ym1__erfeng_"..tostring(i),0)
                end
            end
        end]]--
        
        local cards = room:getCardsFromPileByRule(".|.|spade,club", 3, "discardPile")
        if #cards > 0 then
            room:setPlayerMark(player, "ym1__erfeng_cards", cards)
            local _, ret = room:askToUseActiveSkill(player,{
                skill_name = "ym1__erfeng_give",
                prompt = "#ym1__erfeng-yiji",
                cancelable = true,
                no_indicate = true,
            })
            if ret then
                for _, id in ipairs(ret.cards) do
                  table.removeOne(cards, id)
                end
                room:moveCardTo(ret.cards, Card.PlayerHand, room:getPlayerById(ret.targets[1].id), fk.ReasonGive,
                erfeng.name, nil, false, player.id, nil, player.id)
            end
            room:setPlayerMark(player, "ym1__erfeng_cards", 0)

            if #cards > 0 then
                room:setPlayerMark(to, "ym1__erfeng_cards", cards)
                local _, ret = room:askToUseActiveSkill(to,{
                    skill_name = "ym1__erfeng_give",
                    prompt = "#ym1__erfeng-yiji",
                    cancelable = true,
                    no_indicate = true,
                })
                if ret then
                    for _, id in ipairs(ret.cards) do
                    table.removeOne(cards, id)
                    end
                    room:moveCardTo(ret.cards, Card.PlayerHand, room:getPlayerById(ret.targets[1].id), fk.ReasonGive,
                    erfeng.name, nil, false, to.id, nil, to.id)
                end
                room:setPlayerMark(to, "ym1__erfeng_cards", 0)

                room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, erfeng.name, nil, true, player.id)
            end
        end

        local to_skills = {}
        for _, skill in ipairs(to.player_skills) do
            if skill:isPlayerSkill(to) and not skill.name:startsWith("#") and not skill.attached_equip and not table.contains(erfeng_ban,skill.name) then
                table.insert(to_skills, skill.name)
            end
        end
        if #to_skills == 0 then return end
        table.insert(to_skills,"Cancel")
        local choice = room:askToChoice(to, {
            choices = to_skills,
            skill_name = erfeng.name,
            prompt = "#ym1__erfeng-choice::"..player.id,
            detailed = true,
        })
        if choice ~= "Cancel" then
            local skills = type(player:getMark("ym1__erfeng")) == "table" and player:getMark("ym1__erfeng") or {}
            local current = table.find(player.room.alive_players, function(p) return p.phase ~= Player.NotActive end)
            if current == nil then
                current = table.find(player.room.alive_players, function(p) return p.seat == 1 end)
            end
            if current ~= nil then
                local bool = true
                for i, v in ipairs(skills) do
                    if v[1] == current.id then
                        if not table.contains(v[2],choice) then
                            table.insert(v[2],choice)
                        end
                        bool = false
                        break
                    end
                end
                if bool == true then
                    table.insert(skills,{current.id,{choice}})
                end
            end
            room:handleAddLoseSkills(player, choice, nil, true, false)
            for i, v in ipairs(skills) do
                local cp = room:getPlayerById(v[1])
                if #v[2] > 0 then
                    local realNames = table.map(table.filter(v[2],function (s)
                        return player:hasSkill(s)
                    end), Util.TranslateMapper)
                    local loseNames = table.map(table.filter(v[2],function (s)
                        return not player:hasSkill(s)
                    end), Util.TranslateMapper)
                    if #realNames > 0 then
                        room:setPlayerMark(player,"@ym1__erfeng_"..tostring(i),Fk:translate(cp.general).." <font color='burlywood'>" .. table.concat(realNames, " ") .. "</font>")
                    else
                        room:setPlayerMark(player,"@ym1__erfeng_"..tostring(i),Fk:translate(cp.general).." <font color='gray'>" .. table.concat(loseNames, " ") .. "</font>")
                    end
                    --room:setPlayerMark(player,"@ym1__erfeng_"..tostring(i),Fk:translate(cp.general).." <font color='burlywood'>" .. table.concat(realNames, " ") .. "</font>".." <font color='gray'>" .. table.concat(loseNames, " ") .. "</font>")
                else
                    room:setPlayerMark(player,"@ym1__erfeng_"..tostring(i),0)
                end
            end

            room:setPlayerMark(player,"ym1__erfeng",skills)
            --room:setPlayerMark(player,"ym1__erfeng-turn",choice)
        end
    end,
})
erfeng:addEffect(fk.EventPhaseStart,{
    can_trigger = function (self, event, target, player, data)
        return player:hasSkill("ym1__erfeng") and target ~= player and target.phase == Player.Start and player:usedSkillTimes("ym1__erfeng",Player.HistoryRound) < 2
    end,
    on_cost = function (self, event, target, player, data)
        local room = player.room
        local result = room:askToUseActiveSkill(player,{
            skill_name = "ym1__erfeng",
            prompt = "#ym1__erfeng-prompt",
            cancelable = true
        })
        if result == true then
            local num = player:usedSkillTimes("ym1__erfeng",Player.HistoryRound)
            player:setSkillUseHistory("ym1__erfeng",num + 1,Player.HistoryRound)
        end
    end,
})
erfeng:addEffect(fk.TurnStart,{
    can_refresh = function (self, event, target, player, data)
        local skills = type(player:getMark("ym1__erfeng")) == "table" and player:getMark("ym1__erfeng") or {}
        if #skills == 0 then return end
        for _, v in ipairs(skills) do
            if target.id == v[1] then
                return true
            end
        end
    end,
    on_refresh = function (self, event, target, player, data)
        local room = player.room
        local skills = type(player:getMark("ym1__erfeng")) == "table" and player:getMark("ym1__erfeng") or {}
        for _, v in ipairs(skills) do
            if v[1] == target.id then
                for _, s in ipairs(v[2]) do
                    room:handleAddLoseSkills(player,"-"..s,nil,true,false)
                end
                v[2] = {}
            end
        end
        for i, v in ipairs(skills) do
            local current = room:getPlayerById(v[1])
            if #v[2] > 0 then
                local realNames = table.map(table.filter(v[2],function (s)
                    return player:hasSkill(s)
                end), Util.TranslateMapper)
                local loseNames = table.map(table.filter(v[2],function (s)
                    return not player:hasSkill(s)
                end), Util.TranslateMapper)
                if #realNames > 0 then
                    room:setPlayerMark(player,"@ym1__erfeng_"..tostring(i),Fk:translate(current.general).." <font color='burlywood'>" .. table.concat(realNames, " ") .. "</font>")
                else
                    room:setPlayerMark(player,"@ym1__erfeng_"..tostring(i),Fk:translate(current.general).." <font color='gray'>" .. table.concat(loseNames, " ") .. "</font>")
                end
                --room:setPlayerMark(player,"@ym1__erfeng_"..tostring(i),Fk:translate(current.general).." <font color='burlywood'>" .. table.concat(realNames, " ") .. "</font>".." <font color='gray'>" .. table.concat(loseNames, " ") .. "</font>")
            else
                room:setPlayerMark(player,"@ym1__erfeng_"..tostring(i),0)
            end
        end

        room:setPlayerMark(player,"ym1__erfeng",skills)
    end,
})
erfeng:addEffect(fk.EventLoseSkill,{
    can_refresh = function (self, event, target, player, data)
        local skills = type(player:getMark("ym1__erfeng")) == "table" and player:getMark("ym1__erfeng") or {}
        if #skills == 0 then return end
        if player == target then
            for _, v in ipairs(skills) do
                for _, s in ipairs(v[2]) do
                    if not player:hasSkill(s) then
                        return true
                    end
                end
            end
        end
    end,
    on_refresh = function (self, event, target, player, data)
        local room = player.room
        local skills = type(player:getMark("ym1__erfeng")) == "table" and player:getMark("ym1__erfeng") or {}
        for i, v in ipairs(skills) do
            local current = room:getPlayerById(v[1])
            if #v[2] > 0 then
                local realNames = table.map(table.filter(v[2],function (s)
                    return player:hasSkill(s)
                end), Util.TranslateMapper)
                local loseNames = table.map(table.filter(v[2],function (s)
                    return not player:hasSkill(s)
                end), Util.TranslateMapper)
                if #realNames > 0 then
                    room:setPlayerMark(player,"@ym1__erfeng_"..tostring(i),Fk:translate(current.general).." <font color='burlywood'>" .. table.concat(realNames, " ") .. "</font>")
                else
                    room:setPlayerMark(player,"@ym1__erfeng_"..tostring(i),Fk:translate(current.general).." <font color='gray'>" .. table.concat(loseNames, " ") .. "</font>")
                end
                --room:setPlayerMark(player,"@ym1__erfeng_"..tostring(i),Fk:translate(current.general).." <font color='burlywood'>" .. table.concat(realNames, " ") .. "</font>".." <font color='gray'>" .. table.concat(loseNames, " ") .. "</font>")
            else
                room:setPlayerMark(player,"@ym1__erfeng_"..tostring(i),0)
            end
        end

        room:setPlayerMark(player,"ym1__erfeng",skills)
    end,
})
erfeng:addEffect(fk.SkillEffect,{
    can_refresh = function (self, event, target, player, data)
        local skills = type(player:getMark("ym1__erfeng")) == "table" and player:getMark("ym1__erfeng") or {}
        if #skills == 0 then return end
        if player == target and player:hasSkill(data.skill) and not data.skill:getSkeleton().name:startsWith("#") then
            for _, v in ipairs(skills) do
                for _, s in ipairs(v[2]) do
                    if s == data.skill:getSkeleton().name then
                        return true
                    end
                end
            end
        end
    end,
    on_refresh = function (self, event, target, player, data)
        local room = player.room
        player:broadcastSkillInvoke("ym1__erfeng",math.random(3,4))
    end,
})

Fk:loadTranslationTable{
    ["ym1__erfeng"] = "貮逢",
    ["ym1__erfeng_give"] = "貮逢",
    ["#ym1__erfeng_trigger"] = "貮逢",
    ["@ym1__erfeng_1"] = "",
    ["@ym1__erfeng_2"] = "",
    ["@ym1__erfeng_3"] = "",
    ["@ym1__erfeng_4"] = "",
    ["@ym1__erfeng_5"] = "",
    ["@ym1__erfeng_6"] = "",
    ["@ym1__erfeng_7"] = "",
    ["@ym1__erfeng_8"] = "",
    ["@ym1__erfeng_9"] = "",
    ["@ym1__erfeng_10"] = "",
    [":ym1__erfeng"] = "每轮限两次，其他角色准备阶段或出牌阶段限一次，你可以失去最下端的一个技能，发现弃牌堆的黑色牌并与一名其他角色各分配至多一张，然后其可声明其的一个技能，令你获得至当前回合角色的下个回合开始。"..
    "<br><font color='grey'>ban技能池：<a href=':steam__qisu'>弃俗</a>、<a href=':rfenghou__jianwang'>鉴往</a>、<a href=':rfenghou__bidao'>彼道</a>",
    [":steam__qisu"] = "你可以弃置三张牌并视为使用【桃】或【洞烛先机】，然后摸本次弃置黑色牌数张牌。",
    [":rfenghou__jianwang"] = "限定技，出牌阶段，你可以与半数角色议事（向上取整），然后使用其中一张议事牌，若此牌颜色与议事结果：相同，你令上次发动的技能视为未发动；不同，下次技能发动后视为未发动。",
    [":rfenghou__bidao"] = "以你为目标的牌结算后，你可以获得并明置之，若你未使用或打出牌响应此牌，你需弃置一张同花色牌。你使用明置牌只能指定手牌数"..
    "等于你的角色为目标，若体力值也相等，则不计次数。",
    
    ["#ym1__erfeng-yiji"] = "貮逢：你可以分配其中一张牌",
    ["#ym1__erfeng-prompt"] = "是否发动 貮逢，失去最下端的一个技能并有机会分配黑色牌和再获得一个技能",
    ["#ym1__erfeng-choice"] = "貮逢：你可以令%dest获得其中一个技能",

    ["$ym1__erfeng1"] = "君行其道，臣恪其守，君命臣从，何贰之有？",
    ["$ym1__erfeng2"] = "谓我大失物情？物情不足恤，岂不更闻有上意~",
    ["$ym1__erfeng3"] = "茂先好才气！然台辅之位，非尔所宜居也~",
    ["$ym1__erfeng4"] = "承祚好笔头，既作国史诸卷，且任长广太守。",
    ["$daojie_ym1z__xunxu1"] = "人臣不密，必致失身，安可自取祸由？",
    ["$daojie_ym1z__xunxu2"] = "树私背公，必遭天诛，当戒之，当戒之！",
}
return erfeng